var slideout;

function addValueToAttr(certusNgModelName, c, content, attrs) {

    if (certusNgModelName.indexOf("\.") > -1) {
        var split = certusNgModelName.split(".");
        var jj = 0;
        var nowNode = c;
        for (jj = 0; jj < split.length - 1; jj++) {
            if (nowNode[split[jj]]) {
                nowNode = nowNode[split[jj]]
            } else {
                nowNode[split[jj]] = {}
                nowNode = nowNode[split[jj]]
            }

        }
        nowNode[split[jj]] = content;
    } else {
        nowNode = c
        nowNode[attrs.ngModel] = content;
    }
}

function loopAssignment(scope, attrs, content) {
    var nowNode = scope;
    var split = attrs.split("\.");
    for (jj = 0; jj < split.length - 1; jj++) {
        if (nowNode) {
            if (angular.isUndefined(nowNode[split[jj]])) {
                nowNode[split[jj]] = {}
            }
            nowNode = nowNode[split[jj]]
        } else {
            nowNode = {};
        }
    }
    if (!nowNode) {
        nowNode = {};
    }
    return nowNode[split[jj]] = content;
}

function acquireAttrToEntity(scope, attrs) {
    var nowNode = scope;
    var split = attrs.split("\.");
    for (jj = 0; jj < split.length - 1; jj++) {
        if (nowNode) {
            nowNode = nowNode[split[jj]]
        } else {
            return undefined;
        }
    }
    if (!nowNode) {
        return undefined;
    }
    return nowNode[split[jj]];
}

function directTo(newValue, scope, replace, attr) {
    if (scope.userSelect) {
        for (var xxxx in scope.userSelect) {
            var name = scope.userSelect[xxxx].userName;

            if (name.substring(0, name.indexOf("(")) == newValue) {
                addValueToAttr(replace, scope, scope.userSelect[xxxx].userId, attr);
                addValueToAttr("bid.specifiedMan", scope, scope.userSelect[xxxx].userId, attr);
                addValueToAttr(attr.userDatalist, scope, scope.userSelect[xxxx].userName, attr);
            }
        }
    }
}

var certusApp = angular.module('certusApp', ['ng', 'ngRoute', 'ngAnimate', "ngCookies", 'ui.bootstrap', 'ui.tree', 'angular-loading-bar'], function ($compileProvider) {
    $compileProvider.directive('compile', function ($compile) {
        return function (scope, element, attrs) {
            scope.$watch(
                function (scope) {
                    return scope.$eval(attrs.compile);
                    // return attrs.compile;
                }, function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                }
            )
        }
    })
    $compileProvider.directive('compilejs', function ($compile) {
        return function (scope, element, attrs) {
            scope.$watch(
                function (scope) {
                    return scope.$eval(attrs.compilejs);
                }, function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                }
            )
        }
    })
    $compileProvider.directive("bidSub", function ($compile, $http) {
        return function (scope, element, attrs) {
            scope.$watch(
                attrs.ngModel, function (a, b, c) {

                    if (a) {

                        // $CertusFormAjax = $certusUtils;

                        $http({
                            // url: "proinfo/edit/edit-compact.html",
                            url: "proinfo/edit/edit-bid-sub.html",
                            method: 'GET',
                            data: {},
                            dataType: "application/json"
                        }).then(function (bb) {

                            scope.bidInfo = {};
                            scope.bidInfo.bid = a.bid;
                            scope.index = attrs.bbb;
                            $CertusJsonAjax.ajax("queryBidProcessTrace", {
                                "processInstanceId": a.phaseInfo[0].processInstanceId,
                                "stepInstanceId": a.phaseInfo[0].stepInstanceId
                            }, function (res) {
                                scope["processs"] = res.data;
                                scope.notInit = 'notInit';
                                scope.$on('$viewContentLoaded', function () {
                                    alert('1');

                                });
                                scope.initSubBidGrid = function (a, b) {

                                    scope.notInit = 'init'
// 投标文件
                                    $$scope.$emit('to-parent', a, b);
                                }
                                // $$scope.now = a;
                                element.html(bb.data);
                                scope.file = a.file;
                                $compile(element.contents())(scope);
                            })


                        }, function (b) {
                        })
                    }


                }
            )
        }
    })

    // $compileProvider.directive('certusAnLimitLength', function ($compile) {
    //
    //     return function (scope, element, attrs) {
    //         var scope2 = scope[attrs.ngModel];
    //         scope.$watch(
    //             function (scope2) {
    //                 if (attrs.certusAnLimitLength) {
    //                     if (attrs.certusAnLimitLength % 1 === 0) {
    //                         ;
    //                         var certusLimitLength = parseInt(attrs.certusAnLimitLength);
    //                         scope.certusNgModelName = attrs.ngModel;
    //                         var content = scope.$eval(attrs.ngModel);
    //                         if (content && certusLimitLength > 0) {
    //                             var length = content.length;
    //                             if (length > certusLimitLength) {
    //                                 content = content.substring(0, certusLimitLength);
    //                                 layer.msg("字数限制 " + certusLimitLength + "");
    //                                 scope[attrs.ngModel] = content;
    //
    //                             }
    //                         }
    //                         return scope2;
    //                     }
    //                 }
    //                 layer.msg("限制 字段 长度 配置 有误");
    //                 return scope2;
    //             }, function (value) {
    //                 //啥也不干 yeah
    //                 ;
    //             }
    //         )
    //     }
    // })
    $compileProvider.directive('certusVetoRepo', function (CertusJsonAjax, CertusFormAjax) {
        return function (scope, element, attrs) {
            if (attrs.certusVetoRepo) {
                CertusFormAjax.ajax("certusVetoRepo", {
                    processInstanceId: CertusJsonAjax.getProInstanceId(),

                    repo: attrs.certusVetoRepo
                }, function (res) {

                })
            }
        }
    })

    $compileProvider.directive('certusEmail', function ($compile) {
        return function (scope, element, attrs) {
            var certusLimitLength = parseInt(attrs.certusPhoneNumber);
            var scope2 = scope[attrs.ngModel];

            $(element).on("blur", function () {
                var certusNgModelName = attrs.ngModel;
                var content = scope.$eval(attrs.ngModel);
                var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
                var names = content.match(reg);
                if (names) {
                    content = names[0];
                } else {
                    content = "";
                    layer.msg("邮箱格式错误，请重填")
                }

                var nowNode = scope
                if (certusNgModelName.indexOf("\.") > -1) {
                    var split = certusNgModelName.split(".");
                    var jj = 0
                    for (jj = 0; jj < split.length - 1; jj++) {
                        nowNode = nowNode[split[jj]]
                    }
                    nowNode[split[jj]] = content;
                } else {
                    nowNode[attrs.ngModel] = content;
                }
            })
        }
    })

    $compileProvider.directive('certusPhoneNumber', function ($compile) {
        return function (scope, element, attrs) {
            var certusLimitLength = parseInt(attrs.certusPhoneNumber);
            var scope2 = scope[attrs.ngModel];

            $(element).on("blur", function () {
                var certusNgModelName = attrs.ngModel;
                var content = scope.$eval(attrs.ngModel);
                // var reg = /^1[3|4|5|8][0-9]\d{4,8}$/;
                var reg = /^1[34578]\d{9}$/;
                var names = content.match(reg);
                if (names) {
                    content = names.join("");
                } else {
                    content = "";
                    layer.msg("请填入正确电话号码")
                }
                var nowNode = scope
                if (certusNgModelName.indexOf("\.") > -1) {
                    var split = certusNgModelName.split(".");
                    var jj = 0
                    for (jj = 0; jj < split.length - 1; jj++) {
                        nowNode = nowNode[split[jj]]
                    }
                    nowNode[split[jj]] = content;
                } else {
                    nowNode[attrs.ngModel] = content;
                }
            })
        }
    })

    $compileProvider.directive('certusAnLimitLength', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {
                    if (attrs.certusAnLimitLength) {
                        if (attrs.certusAnLimitLength % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.certusAnLimitLength);
                            var certusNgModelName = attrs.ngModel;
                            var content = scope.$eval(attrs.ngModel);
                            if (content && certusLimitLength > 0) {
                                var length = content.length;
                                if (length > certusLimitLength) {
                                    content = content.substring(0, certusLimitLength);
                                    layer.msg("字数限制 " + certusLimitLength + "");
                                    var nowNode = c;
                                    if (certusNgModelName.indexOf("\.") > -1) {
                                        var split = certusNgModelName.split(".");
                                        var jj = 0
                                        for (jj = 0; jj < split.length - 1; jj++) {
                                            nowNode = nowNode[split[jj]]
                                        }
                                        nowNode[split[jj]] = content;
                                    } else {
                                        nowNode[attrs.ngModel] = content;
                                    }
                                }
                            }
                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })

    $compileProvider.directive('baseCompanyPact', function ($compile, CertusFormAjax) {
        return function (scope, element, attrs) {
            scope.contractInfo.compCodeSelect = [
                {"name":"赛特斯信息科技股份有公司         ","code":"赛特斯信息科技股份有公司        "},
                {"name":"上海赛特斯信息科技股份有限公司   ","code":"上海赛特斯信息科技股份有限公司  "},
                {"name":"上海美琦浦悦众创空间管理有限公司 ","code":"上海美琦浦悦众创空间管理有限公司"},
                {"name":"北京赛特斯信息科技股份有限公司   ","code":"北京赛特斯信息科技股份有限公司  "},
                {"name":"南通美琦湳悦通讯科技有限公司     ","code":"南通美琦湳悦通讯科技有限公司    "},
                {"name":"广东赛特斯信息科技有限公司       ","code":"广东赛特斯信息科技有限公司      "},
                {"name":"netelastic. System Inc           ","code":"netelastic. System Inc          "},
                {"name":"上海浩方信息技术有限公司         ","code":"上海浩方信息技术有限公司        "},
                {"name":"上海浩方科技有限公司             ","code":"上海浩方科技有限公司            "}
            ]
        }
    })

    $compileProvider.directive('baseSelectByUrl', function ($compile, CertusFormAjax) {
        return function (scope, element, attrs) {

            //attrs 中 url param

            //attrs 中 url param
            scope.$watch(attrs.ngModel, function (newVlaue, oldvalue, scope) {
                if (attrs.baseSelectByUrl) {

                    CertusFormAjax.ajax(attrs.baseSelectByUrl, {}, function (res) {
                        //
                        loopAssignment(scope, attrs.ngModel + "Select", res.data);
                    })
                    //watch 结束
                } else {
                    console.log("联动数据不全，请检查");
                }
            })
        }
    })

    $compileProvider.directive('linkageQueryByUrl', function ($compile, CertusFormAjax) {
        return function (scope, element, attrs) {

            //attrs 中 url param

            if (attrs.linkageQueryByUrl && attrs.linkageQueryAttrs) {
                var params = attrs.linkageQueryAttrs.split(",");
                scope.$watch(params[params.length - 1], function (a, b, c) {
                    var param = {};
                    for (var i = 0; i < params.length; i++) {
                        var a2 = acquireAttrToEntity(scope, params[i]);

                        if (!angular.isUndefined(a2)) {
                            param[params[i]] = a2;
                        } else {
                            return;
                        }
                    }
                    CertusFormAjax.ajax(attrs.linkageQueryByUrl, param, function (res) {
                        //
                        loopAssignment(scope, attrs.ngModel + "Select", res.data);
                    })
                    //watch 结束
                })

            } else {
                console.log("联动数据不全，请检查");
            }
        }
    })

    $compileProvider.directive("linkageQueryAttrs", function ($compile) {
        return function (scope, element, attrs) {

        }
    })

    $compileProvider.directive('certusAnLimitLengthStr', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {
                    if (attrs.certusAnLimitLengthStr) {
                        if (attrs.certusAnLimitLengthStr % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.certusAnLimitLengthStr);
                            var certusNgModelName = attrs.ngModel;
                            var content = scope.$eval(attrs.ngModel);
                            if (content && certusLimitLength > 0) {
                                var length = content.length;
                                if (length > certusLimitLength) {

                                    content = content.replace(/[^\w\.\/]/ig, '');

                                    content = content.substring(0, certusLimitLength);
                                    layer.msg("字数限制 " + certusLimitLength + "");
                                    var nowNode = c;
                                    if (certusNgModelName.indexOf("\.") > -1) {
                                        var split = certusNgModelName.split(".");
                                        var jj = 0
                                        for (jj = 0; jj < split.length - 1; jj++) {
                                            nowNode = nowNode[split[jj]]
                                        }
                                        nowNode[split[jj]] = content;
                                    } else {
                                        nowNode[attrs.ngModel] = content;
                                    }
                                }
                            }
                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })

    $compileProvider.directive('maxValueLimit', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {

                    if (attrs.maxValueLimit) {
                        if (attrs.maxValueLimit % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.maxValueLimit);
                            var certusNgModelName = attrs.ngModel;

                            var content = scope.$eval(attrs.ngModel);

                            if (content && certusLimitLength > 0) {
                                content = content + "";
                                content = content && certusLimitLength > 0 ? content.replace(/^\s+/, '').replace(/\s+$/, '') : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/[^\d.]/g, "") : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/\.{2,}/g, ".") : content; //只保留第一个. 清除多余的
                                content = content && certusLimitLength > 0 ? content.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".") : content;
                                content = content && certusLimitLength > 0 ? content.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : content; //只能输入两个小数
                                if (content.indexOf(".") < 0 && content != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                                    content = parseFloat(content);
                                }
                                var nowNode = c;
                                if (certusNgModelName.indexOf("\.") > -1) {
                                    var split = certusNgModelName.split(".");
                                    var ii = 0
                                    for (ii = 0; ii < split.length - 1; ii++) {
                                        nowNode = nowNode[split[ii]]
                                    }
                                    nowNode[split[ii]] = content;
                                } else {
                                    nowNode[attrs.ngModel] = content;
                                }
                                if (content && certusLimitLength > 0) {

                                    if (content > certusLimitLength) {

                                        content = certusLimitLength;
                                        layer.msg("最大值为  " + certusLimitLength + "");
                                        var nowNode = c;
                                        if (certusNgModelName.indexOf("\.") > -1) {
                                            var split = certusNgModelName.split(".");
                                            var jj = 0
                                            for (jj = 0; jj < split.length - 1; jj++) {
                                                nowNode = nowNode[split[jj]]
                                            }
                                            nowNode[split[jj]] = content;
                                        } else {
                                            nowNode[attrs.ngModel] = content;
                                        }
                                    }
                                }
                            }


                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })


    $compileProvider.directive('certusAnLimitLengthNum', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {

                    if (attrs.certusAnLimitLengthNum) {
                        ;
                        if (attrs.certusAnLimitLengthNum % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.certusAnLimitLengthNum);
                            var certusNgModelName = attrs.ngModel;

                            var content = scope.$eval(attrs.ngModel);

                            if (content && certusLimitLength > 0) {
                                content = content + "";
                                content = content && certusLimitLength > 0 ? content.replace(/^\s+/, '').replace(/\s+$/, '') : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/[^\d.]/g, "") : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/\.{2,}/g, ".") : content; //只保留第一个. 清除多余的
                                content = content && certusLimitLength > 0 ? content.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".") : content;
                                content = content && certusLimitLength > 0 ? content.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : content; //只能输入两个小数
                                if (content.indexOf(".") < 0 && content != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                                    content = parseFloat(content);
                                }
                                var nowNode = c;
                                if (certusNgModelName.indexOf("\.") > -1) {
                                    var split = certusNgModelName.split(".");
                                    var ii = 0
                                    for (ii = 0; ii < split.length - 1; ii++) {
                                        nowNode = nowNode[split[ii]]
                                    }
                                    nowNode[split[ii]] = content;
                                } else {
                                    nowNode[attrs.ngModel] = content;
                                }
                                if (content && certusLimitLength > 0) {

                                    content = (content + "");
                                    var length = content.length;
                                    if (length > certusLimitLength) {

                                        content = content.substring(0, certusLimitLength);
                                        layer.msg("字数限制 " + certusLimitLength + "");
                                        var nowNode = c;
                                        if (certusNgModelName.indexOf("\.") > -1) {
                                            var split = certusNgModelName.split(".");
                                            var jj = 0
                                            for (jj = 0; jj < split.length - 1; jj++) {
                                                nowNode = nowNode[split[jj]]
                                            }
                                            nowNode[split[jj]] = content;
                                        } else {
                                            nowNode[attrs.ngModel] = content;
                                        }
                                    }
                                }
                            }


                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })

    $compileProvider.directive('certusAnLimitLengthInt', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {

                    if (attrs.certusAnLimitLengthInt) {
                        ;
                        if (attrs.certusAnLimitLengthInt % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.certusAnLimitLengthInt);
                            var certusNgModelName = attrs.ngModel;

                            var content = scope.$eval(attrs.ngModel);

                            if (content && certusLimitLength > 0) {
                                content = content + "";
                                content = content && certusLimitLength > 0 ? content.replace(/^\s+/, '').replace(/\s+$/, '') : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/[^\d]/g, "") : content; //清除“数字”和“.”以外的字符
                                content = content && certusLimitLength > 0 ? content.replace(/\.{2,}/g, ".") : content; //只保留第一个. 清除多余的
                                content = content && certusLimitLength > 0 ? content.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".") : content;
                                // content = content && certusLimitLength > 0 ? content.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3') : content; //只能输入两个小数
                                if (content.indexOf(".") < 0 && content != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                                    content = parseInt(content);
                                }
                                var nowNode = c;
                                if (certusNgModelName.indexOf("\.") > -1) {
                                    var split = certusNgModelName.split(".");
                                    var ii = 0
                                    for (ii = 0; ii < split.length - 1; ii++) {
                                        nowNode = nowNode[split[ii]]
                                    }
                                    nowNode[split[ii]] = content;
                                } else {
                                    nowNode[attrs.ngModel] = content;
                                }
                                if (content && certusLimitLength > 0) {

                                    content = (content + "");
                                    var length = content.length;
                                    if (length > certusLimitLength) {

                                        content = content.substring(0, certusLimitLength);
                                        layer.msg("字数限制 " + certusLimitLength + "");
                                        var nowNode = c;
                                        if (certusNgModelName.indexOf("\.") > -1) {
                                            var split = certusNgModelName.split(".");
                                            var jj = 0
                                            for (jj = 0; jj < split.length - 1; jj++) {
                                                nowNode = nowNode[split[jj]]
                                            }
                                            nowNode[split[jj]] = content;
                                        } else {
                                            nowNode[attrs.ngModel] = content;
                                        }
                                    }
                                }
                            }


                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })
    /*    $compileProvider.directive("toggleMainMenu", function () {
     return {
     restrict: "A",
     link: function (scope, elem, attrs) {
     slideout = new Slideout({
     'panel': document.getElementById('panel'),
     'menu': document.getElementById('menu'),
     'padding': 256,
     'tolerance': 70,
     'touch': false
     });
     $(elem).click(function () {


     slideout.toggle(scope);

     // CertusFormAjax.initBaseAuthority(scope);
     });
     }
     }
     });

     $compileProvider.directive("mainMenuShow", function ($cookies) {
     return {
     restrict: "A",
     link: function (scope, elem, attrs) {
     scope.$on("mainMenuDis",function(){

     var access_token = $cookies.get("access_token");
     var baseUrl = $cookies.get("baseUrl");
     var token_type = $cookies.get("token_type");
     $.ajax({
     url: baseUrl + 'baseAuthority' + "?access_token=" + access_token,
     type: 'POST',
     crossDomain: true,
     cache: false,
     data: {},
     async: false,
     processData: false,
     contentType: "application/json",
     dataType: "json",
     headers: {
     'Authorization': token_type + " " + access_token
     }
     }).done(function (res) {
     scope.autho = res;
     attrs['autho'] = res;
     }).fail(function (res, a, b, c) {
     });
     scope.autho = scope.autho
     });
     }
     }
     });*/

    $compileProvider.directive('certusAnLimitLengthStr', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {

                    if (attrs.certusAnLimitLengthStr) {
                        ;
                        if (attrs.certusAnLimitLengthStr % 1 === 0) {
                            var certusLimitLength = parseInt(attrs.certusAnLimitLengthStr);
                            var certusNgModelName = attrs.ngModel;
                            var content = scope.$eval(attrs.ngModel) + "";
                            ;
                            if (content && certusLimitLength > 0) {
                                content = content + "";
                                content = content && certusLimitLength > 0 ? content.replace(/[^\w\.\/]/ig, '') : content; //清除“数字”和“.”以外的字符
                                // if (content.indexOf(".") < 0 && content != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                                //     content = parseFloat(content);
                                // }
                                if (content == "undefined") {
                                    content = "";
                                } else {

                                    var nowNode = c;
                                    var split2 = attrs.ngModel.split("\.");
                                    var c2 = c;
                                    for (var i = 0; i < split2.length - 1; i++) {
                                        c2 = c2[split2[i]];
                                    }

                                    c2[split2[split2.length - 1]] = content;
                                }

                            }
                            if (content && certusLimitLength > 0) {
                                var length = content.length;
                                if (length > certusLimitLength) {
                                    content = content.substring(0, certusLimitLength);
                                    layer.msg("字数限制 " + certusLimitLength + "");
                                    var nowNode = c;
                                    if (certusNgModelName.indexOf("\.") > -1) {
                                        var split = certusNgModelName.split(".");
                                        var jj = 0
                                        for (jj = 0; jj < split.length - 1; jj++) {
                                            nowNode = nowNode[split[jj]]
                                        }
                                        nowNode[split[jj]] = content;
                                    } else {
                                        nowNode[attrs.ngModel] = content;
                                    }
                                }
                            }

                        }
                    } else {
                        layer.msg("限制 字段 长度 配置 有误");
                    }
                }
            )
        }
    })

    $compileProvider.directive('certusAnInt', function ($compile) {
        return function (scope, element, attrs) {
            var scope2 = scope[attrs.ngModel];
            scope.$watch(attrs.ngModel,
                function (a, b, c) {

                    var content = scope.$eval(attrs.ngModel);
                    var certusNgModelName = attrs.ngModel;
                    content = content.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
                    content = content.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
                    content = content.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
                    content = content.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
                    if (content.indexOf(".") < 0 && content != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                        content = parseFloat(content);
                    }
                    addValueToAttr(certusNgModelName, c, content, attrs);
                }
            )
        }
    })

    $compileProvider.directive('customNameList', function ($compile, CertusJsonAjax) {
        return function (scope, element, attrs) {
            var projNames = attrs.customNameList.split(",");
            //先获取 html
            // CertusJsonAjax.ajax("customAll",{},function(res){
            CertusJsonAjax.ajax("listCustomers", {}, function (res) {
                var nhlTeams = [];
                for (var i = 0; i < res.data.length; i++) {
                    var arr = {};
                    arr.name = res.data[i].cusName;
                    arr.id = res.data[i].comId;
                    nhlTeams.push(arr);
                }
                //自动补全
                var nhl = $.map(nhlTeams, function (obj) {
                    return {
                        value: obj.name,
                        data: {customName: obj.name, customId: obj.id}
                    };
                });
                // Initialize autocomplete with local lookup:
                $('#' + projNames[2]).devbridgeAutocomplete({
                    lookup: nhl,
                    minChars: 1,
                    onSelect: function (suggestion) {

                        // $("#projId").val(suggestion.data.projId);
                        $('#' + projNames[2]).val(suggestion.data.customName);
                        // $$scope.authoEdit.baseInfo.userId = suggestion.data.projId;
                        debugger;
                        addValueToAttr(projNames[0], scope, suggestion.data.customId, attrs);
                        // $$scope.authoEdit.baseInfo.userName = suggestion.data.proName;
                        addValueToAttr(projNames[1], scope, suggestion.data.customName, attrs);
                        scope.$apply();
                    },
                    showNoSuggestionNotice: true,
                    noSuggestionNotice: 'Sorry, no matching results'
                });

            })
        }
    })

    $compileProvider.directive('projNameList', function ($compile, CertusJsonAjax) {
        return function (scope, element, attrs) {
            var projNames = attrs.projNameList.split(",");
            //先获取 html

            CertusJsonAjax.ajaxNode("custName", "list", {}, function (data) {
                var nhlTeams = [];
                for (var i = 0; i < data.length; i++) {
                    var arr = {};
                    arr.name = data[i].proName;
                    arr.id = data[i].id;
                    nhlTeams.push(arr);
                }
                //自动补全
                var nhl = $.map(nhlTeams, function (obj) {
                    return {
                        value: obj.name,
                        data: {projId: obj.id, projName: obj.name}
                    };
                });
                // Initialize autocomplete with local lookup:
                $('#projNameId').devbridgeAutocomplete({
                    lookup: nhl,
                    minChars: 1,
                    onSelect: function (suggestion) {

                        // $("#projId").val(suggestion.data.projId);
                        $("#projNameId").val(suggestion.data.projName);
                        // $$scope.authoEdit.baseInfo.userId = suggestion.data.projId;
                        addValueToAttr(projNames[0], scope, suggestion.data.projId, attrs);
                        // $$scope.authoEdit.baseInfo.userName = suggestion.data.proName;
                        addValueToAttr(projNames[1], scope, suggestion.data.projName, attrs);
                        scope.$apply();
                    },
                    showNoSuggestionNotice: true,
                    noSuggestionNotice: 'Sorry, no matching results'
                });
            })
        }
    })

    $compileProvider.directive('projNameList', function ($compile, CertusJsonAjax) {
        return function (scope, element, attrs) {
            var projNames = attrs.projNameList.split(",");
            //先获取 html

            CertusJsonAjax.ajaxNode("projName", "list", {}, function (data) {
                var nhlTeams = [];
                for (var i = 0; i < data.length; i++) {
                    var arr = {};
                    arr.name = data[i].proName;
                    arr.id = data[i].id;
                    nhlTeams.push(arr);
                }
                //自动补全
                var nhl = $.map(nhlTeams, function (obj) {
                    return {
                        value: obj.name,
                        data: {projId: obj.id, projName: obj.name}
                    };
                });
                // Initialize autocomplete with local lookup:
                $('#projNameId').devbridgeAutocomplete({
                    lookup: nhl,
                    minChars: 1,
                    onSelect: function (suggestion) {

                        // $("#projId").val(suggestion.data.projId);
                        $("#projNameId").val(suggestion.data.projName);
                        // $$scope.authoEdit.baseInfo.userId = suggestion.data.projId;
                        addValueToAttr(projNames[0], scope, suggestion.data.projId, attrs);
                        // $$scope.authoEdit.baseInfo.userName = suggestion.data.proName;
                        addValueToAttr(projNames[1], scope, suggestion.data.projName, attrs);
                        scope.$apply();
                    },
                    showNoSuggestionNotice: true,
                    noSuggestionNotice: 'Sorry, no matching results'
                });
            })
        }
    })

    $compileProvider.directive('certusRequired', function ($compile) {
        return function (scope, element, attrs) {
            var $2 = $(element);
            var oldClass = $2.attr("class");
            $2.attr("class", oldClass + " needCertus");
            var parent = $(element).parent();
            $2.attr("certusName", parent.parent().find("label").html());

            // parent.parent().find("label").each(function(){
            //     $(this).find("i:eq(0)").html(parent.parent().find("label").html() + "*")
            // })
            parent.parent().find("label:nth-child(1)").html(parent.parent().find("label").html() + "*")
        }
    })

    $compileProvider.directive('certusRequiredPolyfill', function ($compile) {
        return function (scope, element, attrs) {
            var $2 = $(element);
            var oldClass = $2.attr("class");
            $2.attr("class", oldClass + " needCertusPF");
            var parent = $(element).parent();
            $2.attr("certusName", parent.parent().find("label").html());
            parent.parent().find("label:nth-child(1)").html(parent.parent().find("label").html() + "*")
            // parent.parent().find("label").each(function(){
            //     $(this).find("i:eq(0)").html(parent.parent().find("label").html() + "*")
            // })
        }
    })

    $compileProvider.directive('repeatFinish', function () {
        return {
            link: function (scope, element, attr) {
                //console.log(scope.$index)
                if (scope.$last == true) {
                    //console.log('ng-repeat执行完毕');
                    scope.$eval(attr.repeatFinish);
                    scope.$emit('ngRepeatFinished');
                }
            }
        }
    })

    $compileProvider.directive('projDatalist', function ($compile, $http, CertusFormAjax) {
        return {
            link: function (scope, element, attr) {

                $http({
                    url: "templates/workflow/util/DataList.html",
                    method: 'GET',
                    data: {},
                    dataType: "application/json"
                }).then(function (a) {

                    var append = $(element).parent().append(a.data);
                    var find = append.find("datalist");

                    $(element).attr("list", $(find).attr("id"));

                    var replace = attr.projDatalist.replace("Name", "Id");

                    $compile(find.contents())(scope);
                    scope.$watch(attr.projDatalist, function (newValue, old, scope) {
                        if (old && newValue) {
                            if (newValue.indexOf(old) > -1) {
                                nowProjName = newValue;
                                scope.projnameSelect = scope.projnameSelect.filter(isSaveProjName);


                                if (scope.projnameSelect.length) {
                                    if (scope.projnameSelect.length == 1) {
                                        addValueToAttr(replace, scope, scope.projnameSelect[0].projId, attr);
                                        addValueToAttr(attr.projDatalist, scope, scope.projnameSelect[0].projName, attr);
                                    }
                                }
                                if (!scope.projnameSelect || scope.projnameSelect.length == 0) {
                                    addValueToAttr(replace, scope, scope.projnameSelect[0].projId, attr);
                                    addValueToAttr(attr.projDatalist, scope, scope.projnameSelect[0].projName, attr);
                                }
                                return;
                            }
                        }

                        //直接去数据库 查询
                        CertusFormAjax.ajax("relatedProj", {param: newValue}, function (res) {
                            scope.projnameSelect = res.data;
                            if (scope.projnameSelect) {
                                if (scope.projnameSelect.length == 1) {
                                    // scope[attr.projDatalist.replace("Name","Id")] = ;
                                    addValueToAttr(replace, scope, scope.projnameSelect[0].projId, attr);
                                    addValueToAttr(attr.projDatalist, scope, scope.projnameSelect[0].projName, attr);
                                    // scope[attr.projDatalist] = scope.projnameSelect[0].projName;
                                }
                            }
                        })
                    })
                }, function (b) {
                })

            }
        }
    })


    $compileProvider.directive('userDatalist', function ($compile, $http, CertusFormAjax) {
        return {
            link: function (scope, element, attr) {

                $http({
                    url: "templates/workflow/util/userDataList.html",
                    method: 'GET',
                    data: {},
                    dataType: "application/json"
                }).then(function (a) {
                    var append = $(element).parent().append(a.data);
                    var find = append.find("datalist");

                    $(element).attr("list", $(find).attr("id"));

                    var replace = attr.userDatalist.replace("Name", "Id");

                    $compile(find.contents())(scope);
                    scope.$watch(attr.userDatalist, function (newValue, old, scope) {
                        if (newValue && newValue.indexOf("(") > -1) {
                            newValue = newValue.substring(0, newValue.indexOf("("))
                        }
                        if (old && old.indexOf("(") > -1) {
                            old = old.substring(0, old.indexOf("("))
                        }

                        if (old && newValue) {

                            if (newValue.indexOf(old) > -1 && scope.userSelect) {
                                nowPinying = newValue;

                                if (scope.userSelect.length) {

                                    scope.userSelect = scope.userSelect.filter(isSamePinyin);
                                    //如果 相等 则直接
                                    directTo(newValue, scope, replace, attr);

                                    if (scope.userSelect.length == 1) {
                                        addValueToAttr(replace, scope, scope.userSelect[0].userId, attr);
                                        addValueToAttr("bid.specifiedMan", scope, scope.userSelect[0].userId, attr);
                                        addValueToAttr(attr.userDatalist, scope, scope.userSelect[0].userName, attr);
                                    }
                                }
                                if (!scope.userSelect || scope.userSelect.length == 0) {

                                    addValueToAttr(replace, scope, "", attr);
                                    addValueToAttr("bid.specifiedMan", "", attr);
                                    addValueToAttr(attr.userDatalist, scope, "", attr);
                                }
                                return;
                            }
                        }

                        //直接去数据库 查询
                        CertusFormAjax.ajax("relatedUser", {name: newValue}, function (res) {
                            scope.userSelect = res.data;
                            if (scope.userSelect) {
                                if (scope.userSelect.length == 1) {
                                    // scope[attr.projDatalist.replace("Name","Id")] = ;
                                    addValueToAttr(replace, scope, scope.userSelect[0].userId, attr);
                                    addValueToAttr("bid.specifiedMan", scope, scope.userSelect[0].userId, attr);
                                    addValueToAttr(attr.userDatalist, scope, scope.userSelect[0].userName, attr);
                                    // scope[attr.projDatalist] = scope.projnameSelect[0].projName;
                                }
                            }
                        })
                    })
                }, function (b) {
                })

            }
        }
    })

    $compileProvider.directive("toggleMainMenu", function () {
        return {
            restrict: "A",
            link: function (scope, elem, attrs) {

                slideout = new Slideout({
                    'panel': document.getElementById('panel'),
                    'menu': document.getElementById('menu'),
                    'padding': 256,
                    'tolerance': 70,
                    'touch': false
                });
                $(elem).click(function () {


                    slideout.toggle(scope);

                    // CertusFormAjax.initBaseAuthority(scope);
                });
            }
        }
    });

});

var certusApp1 = angular.module('certusApp1', ['certusApp', 'ng', 'ngRoute', 'ngAnimate', "ngCookies", 'ui.bootstrap', 'ui.tree'], function ($compileProvider) {

    $compileProvider.directive("mainMenuShow", function ($cookies) {
        return {
            restrict: "A",
            link: function (scope, elem, attrs) {
                scope.$on("mainMenuDis", function () {

                    var access_token = $cookies.get("access_token");
                    var baseUrl = $cookies.get("baseUrl");
                    var token_type = $cookies.get("token_type");
                    $.ajax({
                        url: baseUrl + 'baseAuthority' + "?access_token=" + access_token,
                        type: 'POST',
                        crossDomain: true,
                        cache: false,
                        data: {},
                        async: false,
                        processData: false,
                        contentType: "application/json",
                        dataType: "json",
                        headers: {
                            'Authorization': token_type + " " + access_token
                        }
                    }).done(function (res) {
                        scope.autho = res;
                        // attrs['autho'] = res;
                    }).fail(function (res, a, b, c) {

                    });
                    scope.autho = scope.autho
                });
            }
        }
    });


});

certusApp.config(['cfpLoadingBarProvider', function (cfpLoadingBarProvider) {
    cfpLoadingBarProvider.includeSpinner = false;
}])

/*基本配置*/
certusApp.controller("mainCtrl", function (CertusFormAjax, $scope, CertusJsonAjax, $location, $http) {

    startMenu(CertusFormAjax, $scope, CertusJsonAjax, $location, $http);
    CertusJsonAjax.initBaseAuthority($scope);
    $scope.logout = function () {
        CertusFormAjax.logout();
    }
    // setInterval(function () {
    //     exec();
    // }, 30000);
    exec();

    function exec() {
        findTsInfo($scope, CertusFormAjax, CertusJsonAjax);
    }
});


// certusApp1.controller("mainCtrl1", function ($scope, $location, $http) {
//
// });


certusApp.filter('traceStatus', function () {
    return function (bb) {
        if (bb == "starting") {
            return "正在进行中";
        } else if (bb == "denied") {
            return "否决";
        } else if (bb == "sentback") {
            return "被退";
        } else if (bb == "others") {
            return "关闭";
        } else if (bb == "completed") {
            return "完成";
        } else if (bb == "approvaling") {
            return "待办";
        }
        return "完成";
    }
})

certusApp.filter("yesorno", function () {
    return function (bb) {
        if (bb == 0) {
            return "否";
        } else if (bb == 1) {
            return "是";
        }
        return "未选择";
    }
})